package com.example.czxyindex.web;

import com.example.czxyindex.web.remote.IUserAction;
import com.yc.czxy.bean.Result;
import com.yc.czxy.bean.user;
import com.yc.czxy.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @program: chengzixy
 * @description:
 * @author:
 * @create: 2021-09-01 00:23
 */
@CrossOrigin
@RestController
@RequestMapping("/userAction")
public class userAction {

    @Autowired
    IUserAction iUserAction;

    @GetMapping("/login")
    public R login(@RequestParam("userName") String userName, @RequestParam("passWord") String passWord, HttpSession session){

        user u=iUserAction.selectForLogin(userName,passWord);
        if (u==null){
            return R.error("登入失败");
        }else {
            session.setAttribute("loginedUser",u);
            System.out.println("============="+session.getId());
            return R.restResult(u,0,"登录成功");
        }

    }

    /**
     * 获取登录信息
     * @param session
     * @return
     */
    @GetMapping("getLoginedUser")
    @CrossOrigin
    public Result<user> getLoginedUser(HttpSession session){

        System.out.println("sessionId:{}"+session.getId());
        user loginedUser= (user) session.getAttribute("loginedUser");
        if (loginedUser==null){
            return  new Result<>(1,"no logined",null);
        }else {
            return  new Result<>(0,"OK",loginedUser);
        }
    }

}
